home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993…ch: Other People's Memory / ADC Developer CD (1993-03) (''Other People's Memory'')_iso / Dev.CD Mar 93.iso / Development Platforms / CSMP Digests / csmp-v1-087.txt / Error.c next >
Encoding:
Text File  |  1992-04-16  |  2.3 KB  |  101 lines

  1. /*
  2.  *        Error.c
  3.  *
  4.  *            -- by Michael Hecht (Michael_Hecht@mac.sas.com)
  5.  *               public domain
  6.  */
  7.  
  8.  
  9. /* This part goes in a header file someplace */
  10. void Substitute( StringPtr targetStr, ConstStr255Param find, ConstStr255Param replace );
  11. void GetErrorString( OSErr err, StringPtr errorStr );
  12.  
  13. enum {
  14.     kErrorStrList = 128,
  15.         kUnknownErrorStr = 1
  16. };
  17.  
  18.  
  19. /* Locate the "find" string in the "targetStr" and replace it with "replace" */
  20. void Substitute( StringPtr targetStr, ConstStr255Param find, ConstStr255Param replace )
  21. {
  22.     StringHandle    target;
  23.     Size            targetSize;
  24.  
  25.  
  26.     target = NewString( targetStr );
  27.     if( !target )
  28.         return;
  29.  
  30.     /* Perform the substitution */
  31.     Munger(( Handle )target, 1, find + 1, *find, replace + 1, *replace );
  32.  
  33.     /* Put it back in the string */
  34.     HLock(( Handle )target );
  35.     targetSize = GetHandleSize(( Handle )target ) - 1;
  36.     if( targetSize > 255 )
  37.         targetSize = 255;
  38.     **target = targetSize;
  39.     BlockMove( *target, targetStr, targetSize + 1 );
  40.     DisposHandle(( Handle )target );
  41. }
  42.  
  43. /* Definition of the 'ERR#' resource structure */
  44. typedef struct {
  45.     OSErr            loRange, hiRange;
  46.     int                strID;
  47. } ErrorMapRecord, *ErrorMapPtr;
  48.  
  49. typedef struct {
  50.     int                count;
  51.     ErrorMapRecord    error[];
  52. } ErrorListRecord, *ErrorListPtr, **ErrorListHandle;
  53.  
  54.  
  55. void GetErrorString( OSErr err, StringPtr errorStr )
  56. {
  57.     ErrorListHandle            theErrorList;
  58.     register int            i, n;
  59.     register ErrorMapPtr    e;
  60.     Str255                    tempStr;
  61.  
  62.  
  63.     /* Assume we won't find a message */
  64.     *errorStr = 0;
  65.  
  66.     /* Access the error mapping list */
  67.     theErrorList = ( ErrorListHandle )Get1Resource( 'ERR#', 128 );
  68.     if( theErrorList ) {
  69.  
  70.         /* Set up for loop */
  71.         n = ( *theErrorList )->count;
  72.         e = ( *theErrorList )->error;
  73.  
  74.         /* Go through the list of ranges */
  75.         for( i = 0; i < n; i++, e++ ) {
  76.  
  77.             /* If not in this range, skip */
  78.             if( err > e->hiRange )
  79.                 continue;
  80.  
  81.             /* If range not found, break */
  82.             if( err < e->loRange )
  83.                 break;
  84.  
  85.             /* Found it! Get the message */
  86.             GetIndString( errorStr, e->strID, err - e->loRange + 1 );
  87.             break;
  88.         }
  89.     }
  90.  
  91.     /* If no message, use "unknown error" message */
  92.     if( *errorStr == 0 ) {
  93.  
  94.         GetIndString( errorStr, kErrorStrList, kUnknownErrorStr );
  95.  
  96.         /* Substitute the error number into the message */
  97.         NumToString(( long )err, tempStr );
  98.         Substitute(( StringPtr )errorStr, ( StringPtr )"\p^ERR", tempStr );
  99.     }
  100. }
  101.